package culton.android.online.cinema;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import culton.android.DetailsActivity;
import culton.android.R;
import culton.android.clicklistener.OnItemListClickListener;
import culton.android.entity.Cinema;

/**
 * This class creates an array adapter for cinemas.
 * It is used by ListActivities that lists cinemas. 
 * **/
public class CinemaArrayAdapter extends ArrayAdapter<Cinema> {

    private ArrayList<Cinema> items;
    private Context mContext;
    private Intent extraInfo;
    
    public CinemaArrayAdapter(Context context, int textViewResourceId, ArrayList<Cinema> items, Intent extraInfo) {
            super(context, textViewResourceId, items);
            this.items = items;
            this.mContext = context;
            this.extraInfo = extraInfo;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.cinemas, null);
        }
        
        Cinema c = items.get(position);
        if (c != null) {
            TextView tt = (TextView) v.findViewById(R.id.toptext);
            if (tt != null)
                  tt.setText(c.getName());                            
        }

        Intent mIntent = null;
        String parentActivity = null;
        boolean isOnline = false;
        
        if (extraInfo != null) {
	        Bundle mBundle = extraInfo.getExtras();
	        isOnline = mBundle.getBoolean("online");
	        parentActivity = mBundle.getString("parent");
        }
        
        if (parentActivity == "cinemasActivity") {
        	mIntent = new Intent(mContext, CinemaMoviesActivity.class);
        	mIntent.putExtra("cinemaID", c.getID());
        	mIntent.putExtra("online", isOnline);
        
        } else {
	        Bundle mBundle = extraInfo.getExtras();
	        mIntent = new Intent(mContext,DetailsActivity.class);
	        mIntent.putExtra("movieID", mBundle.getString("movieID"));
	        mIntent.putExtra("cinemaID", c.getID());
	        mIntent.putExtra("online", isOnline);
	    }      
        
        if (c.getID() != "none")
        	v.setOnClickListener(new OnItemListClickListener(mContext, mIntent));
        
        return v;
    }
    
}